import * as dataCenterService from '../../services/dataCenter';

export default {
  namespace: 'dataCenter',
  state: {
    dataCenterList: [],
    current : 1,
    currentSize : 10,
    total : 0, 
  },
  reducers: {
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
  },
  effects: {
    
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(dataCenterService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { dataCenterList:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *create({ payload },{ call, put }){
      const { body } = yield call(dataCenterService.create,payload);
      yield put({ type: 'reload' });
    },
    *edit({ payload },{ call, put }){
      const { body } = yield call(dataCenterService.edit,payload);
      yield put({ type: 'reload' });
    },
    *remove({ payload },{ call, put }){
      const { body } = yield call(dataCenterService.remove,payload.id);
      yield put({ type: 'reload' });
    },
    *reload(action, { put }) {
      yield put({ type: 'queryList', payload:{ 'name':'' }});
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/basic/dataCenter') {
          dispatch({ type: 'queryList', payload:{ 'name':'' }});
        }
      });
    },
  },
};
